<script src="/js/common/treeSelect.js"></script>
<div class="wrapper wrapper-content ">
    <div class="row" style="margin: 0;">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form class="form-horizontal m-t" id="signupForm">
                        <input id="userId" name="userId" type="hidden" value="${user.userId!}">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">姓名：</label>
                            <div class="col-sm-8">
                                <input id="name" name="name" class="form-control" type="text" value="${user.name!}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">用户名：</label>
                            <div class="col-sm-8">
                                <input id="username" name="username" class="form-control"
                                       type="text" value="${user.username!''}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">部门：</label>
                            <div class="col-sm-8">
                                <input type="hidden" name="deptId" id="deptId" value="${user.deptId!}"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">E-mail：</label>
                            <div class="col-sm-8">
                                <input id="email" name="email" class="form-control"
                                       type="email" value="${user.email!}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">状态:</label>
                            <div class="col-sm-8">
                                <label class="radio-inline"> <input type="radio" name="status" class="i-checks"
                                                                    value="1"
                                                                    <#if '${user.status!}'== '1'>checked</#if>> 正常
                                </label>
                                <label class="radio-inline"> <input type="radio" name="status" value="0"
                                                                    class="i-checks"
                                                                    <#if '${user.status!}'== '0'>checked</#if>> 禁用
                                </label>
                            </div>
                        </div>
                        <input type="hidden" name="roleIds" id="roleIds">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">角色</label>
                            <div class="col-sm-8">
                                <#list roles as role>
                                    <#if role.roleSign?? && role.roleSign=="1">
                                        <#assign role_check = 'checked'>
                                    <#else>
                                        <#assign role_check = ''>
                                    </#if>
                                    <input type="checkbox"  name="role" class="i-checks"
                                           value="${role.roleId}" ${role_check} />${role.roleName}
                                </#list>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    //初始化icheck插件
    $(".i-checks").iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green'
    });
    $(function () {
        validateRule();
        var options = {
            input_name: "deptName",
            get_url: "/system/sysDept/tree",
            value: "${user.deptId!}"
        }
        $("#deptId").combotree(options);
        <#--var deptId="${user.deptId!}";-->
        <#--$("#"+deptId).click;-->
    });

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    function save() {
        $("#roleIds").val(getCheckedRoles());
        var url = "/sys/user/save";
        var userId = $("#userId").val();
        if (userId != '' && userId != undefined) {
            url = "/sys/user/update";
        }
        postAjax(url, $('#signupForm').serialize(), function (data) {
            layer.msg("操作成功");
            reLoad();
            layer.close(current_index);
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        var data = {
            url: "/sys/user/exit", // 后台处理程序
            type: "post", // 数据发送方式
            dataType: "json", // 接受数据格式
            data: { // 要传递的数据
                username: function () {
                    return $("#username").val();
                }
            }
        }
        var userId = $("#userId").val();
        if (userId != '' && userId != undefined) {
            data = false;
        }
        $("#signupForm").validate({
            rules: {
                name: {
                    required: true
                },
                username: {
                    required: true,
                    minlength: 2,
                    remote: data
                },
                deptName: {
                    required: true
                },
                email: {
                    required: true,
                    email: true
                },
                topic: {
                    required: "#newsletter:checked",
                    minlength: 2
                },
                agree: "required"
            },
            messages: {
                name: {
                    required: icon + "请输入姓名"
                },
                username: {
                    required: icon + "请输入您的用户名",
                    minlength: icon + "用户名必须两个字符以上",
                    remote: icon + "用户名已经存在"
                },
                deptName: {
                    required: icon + "请选择部门"
                },
                email: icon + "请输入您的E-mail"
            },
            submitHandler: function (form) {
                save();
            },
            invalidHandler: function (form, validator) {
                return false;
            }
        })
    }

</script>
